# coding:utf-8
import sys;sys.path.append("..")
import re
import argparse

import hashlib

package = '''
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="serverid"

        17
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="datatype"

        convert
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="format"

        jpg
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="id"

        WU_FILE_0
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="name"

        pic.png
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="type"

        image/png
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="lastModifiedDate"

        Thu Jul 26 2018 11:55:45 GMT+0800 (中国标准时间)
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="size"

        523861
        ------WebKitFormBoundaryLeNntdfAGu1IF57E
        Content-Disposition: form-data; name="file"; filename="pic.png"
        Content-Type: image/png


        ------WebKitFormBoundaryLeNntdfAGu1IF57E--

        '''


# 解析上传包为字典,返回post数据字典，以及上传文件name列表
def upload_package_format(package):
    name_list = re.findall(r' name="(.+?)"\n', package)[0:-1]
    value_list = []
    temp_text = package.replace("\n", "").replace(" ", "")
    for name in name_list:
        value_list.append(re.search(r'name="' + name + '"(.+?)--', temp_text).group(1))
    file_name_list = re.findall(r' name="(.+?)";', package)
    return dict(zip(name_list, value_list)), file_name_list


# 将文件以字符串形式读取
def read_file_as_string(file):
    package_file = open(file).readlines()
    package = ""
    for i in package_file:
        package += i
    return package

# 将列表转化为字符串
def list_to_string(strlist):
    strtemp = ""
    for i in strlist:
        strtemp += i
    return strtemp

# 输入内容，返回输入值
# outputstr ： 输出的显示内容
def input_string(outputstr):
    print(outputstr)
    temp_str=''
    temp=''
    while temp != ":q":
        temp=raw_input()
        temp_str+=temp+"\n"
    return temp_str

post_package = '''
    username: 5am3
    password: 5am3
    deviceid: null
    redirect_uri: abcdefg
    '''

# 解析POST数据包
def post_package_format(package):
    package = package.replace(" ", "")
    name_list = re.findall(r'(.+?):', package)
    value_list = re.findall(r':(.+)', package)
    return dict(zip(name_list, value_list))


headers = '''
    Accept: */*
    Accept-Encoding: gzip, deflate
    Accept-Language: zh-CN,zh;q=0.9
    Connection: keep-alive
    Cookie: _ga=GA1.2.624956916.1532411011; dda_user_identifier=f060ceaf-a2ac-4466-be49-2807b4ff8e9c; _gid=GA1.2.24098697.1533049375
    Host: shuiyin.sec.xiaojukeji.com
    Referer: http://s.xiaojukeji.com/?pn=1&cl=all&caller=iportal&query=%E6%89%93%E8%BD%A6%20%E6%B2%A1%E6%9C%89
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
'''


def headers_format(headers):
    name_list = re.findall(r'(.+?): ', headers)
    for i in range(len(name_list)):
        name_list[i] = name_list[i].replace(" ", "")
    value_list = re.findall(r': (.+)', headers)
    return dict(zip(name_list, value_list))


# print headers_format(headers)
# a,b= upload_package_format(package)

def ad_md5(s):
    # 创建md5对象
    hl = hashlib.md5()
    hl.update(s.encode(encoding='utf-8'))

    return hl.hexdigest()




if __name__=="__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("model", help="Select model. headers,post,upload",
                        action="store",choices=["headers","post","upload","h","p","u"])
    parser.add_argument("-f", "--file", help="Read string from file.",
                        action="store")
    args = parser.parse_args()



    if(args.file):
        tempstr = read_file_as_string(args.file)
    else:
        tempstr = input_string("[+] Please input string to format! \n[+] Enter :q to exit")


    if(args.model=="headers" or args.model=="h"):
        formatstr = headers_format(tempstr)
    elif(args.model=="post" or args.model=="p"):
        formatstr = post_package_format(tempstr)
    elif(args.model=="upload" or args.model=="u"):
        formatstr = upload_package_format(tempstr)

    print(formatstr)









